package com.hll.api.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;

/**
 * Scoket返回消息封装
 * @author 何龙
 * @create 2019-07-14 21:32
 */
@Data
public class ScoketMsg {
	/** 状态码 */
	private Integer status;
	/** 错误信息 */
	private String err;
	/** 内容 */
	private Object content;
	private ScoketMsg(Integer status, String err, Object content) {
		this.status = status;
		this.err = err;
		this.content = content;
	}

	/** 成功 */
	public static ScoketMsg success(Object content){
		if (content instanceof String){
			return successStr((String)content);
		}
		return new ScoketMsg(0,null,content);
	}
	/** 返回文本 */
	public static ScoketMsg successStr(String content){
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		object.put("groupType",1);
		object.put("resultType","text");
		JSONObject object1 = new JSONObject();
		object1.put("text",content);
		object.put("values",object1);
		array.add(object);
		return new ScoketMsg(0,null,array);
	}
	/** 返回错误信息 */
	public static ScoketMsg err(String err){
		return new ScoketMsg(2,err,null);
	}
	/** 错误信息 */
	public static ScoketMsg err(Integer status,String err){
		return new ScoketMsg(status,err,null);
	}

	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}
}
